﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FarmacieDAL;
using System.ComponentModel;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for MedicamentInsert.xaml
    /// </summary>
    public partial class MedicamentInsert : UserControl, INotifyPropertyChanged
    {
        private Medicament medicament;
        private Action refresh;

        public Medicament Medicament
        {
            get { return medicament; }
            set
            {
                if (medicament != value)
                {
                    medicament = value;
                    OnPropertyChanged("Medicament");
                }
            }
        }

        public MedicamentInsert()
        {
            InitializeComponent();
            
            buttonAdaugaMedicament.Click += buttonAdaugaMedicament_Click;
            Medicament = new Medicament();
            
            this.DataContext = this;
        }

        public void InitializeControl(Action refresh)
        {
            this.refresh = refresh;
        }

        void buttonAdaugaMedicament_Click(object sender, RoutedEventArgs e)
        {
            FarmacieDALManager.EntitiesContext.AddToMedicament(medicament);
            FarmacieDALManager.EntitiesContext.SaveChanges();

            Medicament = new Medicament();

            if (refresh != null)
            {
                refresh();
            }
        }


        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
